const app = getApp();
const tabList=[
  {name:'水卡入库',id:0},
  {name:'水卡批量入库',id:1},
  {name:'水卡批量开卡',id:2},
  {name:'水卡批量充值',id:3},
]
Page({

  data: {
    tabList,
    curType:0,//0水卡入库 1批量入库 2批量开卡 3批量充值
    startCard: "",//开始卡号
    endCard: "",//终止卡号
    rechargeMoney:0,//充值金额
    sendMoney:0,//赠送金额
    deposit:0,//押金
    showPop:false,
    backData:{},
    hintText:'',
  },
  onLoad(option){
      this.setData({curType:option.type})
      if(option.type==0){
        wx.setNavigationBarTitle({
          title: '水卡入库',
        })
        this.setData({hintText:'入库'})
      }
      if(option.type==1){
        wx.setNavigationBarTitle({
          title: '水卡 批量入库',
        })
        this.setData({hintText:'入库'})
      }
      if(option.type==2){
        wx.setNavigationBarTitle({
          title: '水卡 批量开卡',
        })
        this.setData({hintText:'开卡'})
      }
      if(option.type==3){
        wx.setNavigationBarTitle({
          title: '水卡 批量充值',
        })
        this.setData({hintText:'充值'})
      }
      
  },
  //所有输入...
  inputCard(e) {
      const name = e.currentTarget.dataset.name;
      const is_money = e.currentTarget.dataset.money;
      this.setData({
        [name]:is_money==1?e.detail.value:app.formatCard(e.detail.value)
      })
  },
  // 扫码
  scan: function (e) {
    const name = e.currentTarget.dataset.name;
    wx.scanCode({
      success: (res) => {
        this.setData({
          [name]:app.formatCard(res.result)
        })
      },
      fail: (res) => {
        app.showToast('请扫描正确条形码');
      }
    })
  },
  //普通水卡入库
  confirmBtn0() {
    const card_number = app.trimAll(this.data.cardNum);
    if (card_number === "") {
      app.showToast("请输入水卡号");
      return false;
    }
    wx.showLoading({
      mask: true,
    })
    app.ajax({
      url: "Partner/WaterCard/addCards",
      data: {
        cards: [card_number]
      },
      success: res => {
        wx.hideLoading()
        if (res.data.code == 1000) {
          app.showToast(res.data.message, "success");
          this.setData({
            cardNum: ""
          })
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        wx.hideLoading()
      }
    })
  },
  //普通水卡批量入库
  confirmBtn1() {
    const startCard = app.trimAll(this.data.startCard);
    const endCard = app.trimAll(this.data.endCard);
    if (startCard === "") {
      app.showToast("请输入起始卡号");
      return false;
    }
    if (endCard === "") {
      app.showToast("请输入终止卡号");
      return false;
    }
    if (startCard>endCard) {
      app.showToast("结束卡号必须大于起始卡号!");
      return false;
    }
    wx.showLoading({
      mask: true,
    })
    app.ajax({
      url: "Partner/CardSerial/serialLibrary",
      data: {
        start_serial_number:startCard,
        end_serial_number:endCard
      },
      success: res => {
        wx.hideLoading()
        if (res.data.code == 1000) {
          this.setData({
            showPop: true,
            backData:res.data,
            startCard:'',
            endCard:''
          })
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        wx.hideLoading()
      }
    })
  },
  //批量开卡 || 批量充值     curType 2批量开卡 3批量充值
  confirmBtn23(){
    const startCard = app.trimAll(this.data.startCard);
    const endCard = app.trimAll(this.data.endCard);
    const rechargeMoney=this.data.rechargeMoney;
    const sendMoney=this.data.sendMoney;
    const curType=this.data.curType;
    const deposit=curType==2?this.data.deposit:'';
    const salesman_id = wx.getStorageSync('salesman_id');
    const role = wx.getStorageSync('role');// {"维修端">>role: 1;"销售端">>role: 2; "合伙人端">>role: 3;
    if (startCard === "") {
      app.showToast("请输入起始卡号");
      return false;
    }
    if (endCard === "") {
      app.showToast("请输入终止卡号");
      return false;
    }
    if(curType==3&&rechargeMoney==''){
      app.showToast("请输入充值金额");
      return false;
    }
    if(curType==3&&rechargeMoney==0){
      app.showToast("请输入充值金额不能为0元");
      return false;
    }
    var updata={};
    //role==2  销售端
    if(role==2){
      updata={ 
        start_serial_number:startCard,
        end_serial_number:endCard,
        recharge_money:rechargeMoney,
        give_money:sendMoney,
        salesman_id,
        deposit
      };
    }else{
      updata={ 
        start_serial_number:startCard,
        end_serial_number:endCard,
        recharge_money:rechargeMoney,
        give_money:sendMoney,
        deposit
      };
    }
   
    wx.showLoading({
      mask: true,
    })
    app.ajax({
      url: curType==2?"Partner/CardSerial/addSerialCardAll":"Partner/CardSerial/serialCardRecharge",
      data: updata,
      success: res => {
        wx.hideLoading()
        if (res.data.code == 1000) {
          console.log('获取到的数据',res.data);
          this.setData({
            showPop: true,
            backData:res.data,
            startCard:'',
            endCard:'',
          })
        } else {
          app.showToast(res.data.message)
        }
      },
      error: e => {
        wx.hideLoading()
      }
    })
  },
  //复制按钮
  CopyCloseFun(){
    var error_card=this.data.backData.data.error_card||[];
      error_card=error_card.toString();
    if(error_card.length>0){
      wx.setClipboardData({
        data: error_card,
        success (res) {
        }
      })
    }
    this.setData({
      showPop: false
    })   
  },
  //跳转按钮 curType 2批量开卡 3批量充值 // {"维修端">>role: 1;"销售端">>role: 2; "合伙人端">>role: 3;
  jumpUrl(){
    const curType=this.data.curType;
    
    const salesman_id = wx.getStorageSync('salesman_id');
    const role = wx.getStorageSync('role');
      if(curType==0 || curType==1){
        wx.navigateTo({
          url:'/pages/partner/cardList/cardList'
        })
      }else{
        if(role==2){
          wx.navigateTo({
            url:curType==2?'/promoter/pages/applyList/applyList':'/promoter/pages/rechargeRecord/rechargeRecord'
          })
        }else{
          wx.navigateTo({
            url:curType==2?'/pages/partner/applyList/applyList':'/pages/partner/rechargeRecord/rechargeRecord'
          })
        }
      }
       
  }
})